﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBA.Common;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Model;
using DBA.Web.UI;
using DBA.Bll;
using System.Data;


namespace DBA.Web.StudentManage
{
    public partial class AuditionManage : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["showType"] = UtilsHelper.Decode(Request.Params["type"]);
            if (IsPostBack) return;
            ddlBind();
            txt_Year.Value = System.DateTime.Now.ToString("yyyy");
            IDictionary<String, object> dict = new Dictionary<string, object>();
            dict.Add("Year", txt_Year.Value.Trim());
            switch (ViewState["showType"].ToString())
            {
                case "0": //查看所有教师的服务工作量                    
                    break;
                case "1": //查看当前系的所有的服务工作量
                    dict.Add("Xi_ID", ServerContext.AdminCurrentUser.Xi_ID);
                    li_add.Visible = false;
                    li_del.Visible = false;
                    li_import.Visible = false;   
                    ddlXi.SelectedValue = ServerContext.AdminCurrentUser.Xi_ID.ToString();
                    ddlXi.Enabled = false;
                    break;
                case "2": //查看当前教师自己的服务工作量
                    dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                    li_add.Visible = false;
                    li_del.Visible = false;
                    li_import.Visible = false;   
                    ddlXi.SelectedValue = ServerContext.AdminCurrentUser.Xi_ID.ToString();
                    ddlXi.Enabled = false;
                    break;
            }
            Bind(1, dict);
        }



        protected void linkBtnSearch_Click(object sender, EventArgs e)
        {
            IDictionary<String, object> dict = new Dictionary<String, object>();
            switch (ViewState["showType"].ToString())
            {
                case "0": //查看所有教师的服务工作量                    
                    break;
                case "1": //查看当前系的所有的服务工作量                    
                    break;
                case "2": //查看当前教师自己的服务工作量
                    dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                    break;
            }
            dict.Add("Xi_ID", ddlXi.SelectedValue);
            dict.Add("Name", txt_Name.Text.Trim());
            dict.Add("Year", txt_Year.Value.Trim());
            ViewState["conditions"] = dict;
            Bind(1, dict);
        }


        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            IDictionary<String, object> dict = null;
            if (ViewState["conditions"] != null)
            {
                dict = (IDictionary<String, object>)ViewState["conditions"];
            }
            {
                dict = new Dictionary<string, object>();
                switch (ViewState["showType"].ToString())
                {
                    case "0": //查看所有教师的服务工作量                    
                        break;
                    case "1": //查看当前系的所有的服务工作量                     
                        break;
                    case "2": //查看当前教师自己的服务工作量
                        dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);                    
                        break;
                }
            }
            Bind(e.NewPageIndex, dict);
        }

        private void Bind(int pageIdx, IDictionary<String, object> dict)
        {

            StringBuffer sql = "select A.*,b.Name as TeacherName,c.Name as XiName from (select EmployeeID,[YEAR],    ";
            sql += "isnull(sum(case Grade when 1 then StudentCount else 0 end),0) MasterCount, ";
            sql += "isnull(sum(case Grade when 2 then StudentCount else 0 end),0) DoctorCount, ";
            sql += "isnull(sum(case Grade when 3 then StudentCount else 0 end),0) DBACount,";
            sql += "isnull(sum(case Grade when 4 then StudentCount else 0 end),0) MBACount, ";
            sql += "isnull(sum(case Grade when 5 then StudentCount else 0 end),0) EMBACount, ";
            sql += "isnull(sum(case Grade when 6 then StudentCount else 0 end),0) EngineeringCount ";
            sql += " from TeacherReview group by EmployeeID,[Year]) a inner join EmployeeInfo b on a.EmployeeID=b.EmployeeID ";
            sql += " left join Xi c on b.Xi_ID=c.Xi_ID ";
            sql += " where 1=1 ";
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
            switch (ViewState["showType"].ToString())
            {
                case "1": //查看当前系的所有的服务工作量     
                    break;
                case "2"://查看当前教师自己的服务工作量
                    if (!VerificationHelper.IsNullOrEmpty(dict["EmployeeID"]))
                    {
                        sql += " and b.EmployeeID = @EmployeeID";
                        dal.ManagerParameter.AddNewInParameter("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                    }
                    break;
            }
            if (ddlXi.SelectedValue != "-1") 
            {
                sql += " and b.Xi_ID = @Xi_ID";
                dal.ManagerParameter.AddNewInParameter("Xi_ID",ddlXi.SelectedValue);
            }
            if (txt_Name.Text != "")
            {
                sql += " and b.Name like '%'+@Name+'%'";
                dal.ManagerParameter.AddNewInParameter("Name", txt_Name.Text);
            }
            //if (dict.ContainsKey("Year"))
            //{
                sql += " and a.Year = @Year";
                dal.ManagerParameter.AddNewInParameter("Year",txt_Year.Value);
            //}

            int iCount = 0;

            replData.DataSource = dal.ModifyData.GetPageDataTable("(" + sql.ToString() + ")", "*", "", "EmployeeID DESC,[Year] DESC", pageIdx, 10, ManagerSQL.PageViewTableType.MixtureTable, ref iCount);
            replData.DataBind();
            AspNetPager1.RecordCount = iCount;

        }

        protected void linkBtnMaskSearch_Click(object sender, EventArgs e)
        {
            //IDictionary<String, String> dict = new Dictionary<String, String>();
            //dict.Add("UserName", txtMaskMemberId.Text.Trim());
            //ViewState["conditions"] = dict;
            //Bind(1, dict);
        }

        protected void ddlBind()
        {
            BaseCodeDDL.DdlInit(ddlXi, "Xi");//绑定系      
         
        }

        protected void lnkDownload_Click(object sender, EventArgs e)
        {
            IDictionary<String, object> dict = new Dictionary<String, object>();
            switch (ViewState["showType"].ToString())
            {
                case "0": //查看所有教师的服务工作量                    
                    break;             
                case "2": //查看当前教师自己的服务工作量
                    dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                    break;
            }
            dict.Add("Name", txt_Name.Text.Trim());
            dict.Add("Year", txt_Year.Value.Trim());

            StringBuffer sql = "select b.Name as 教师姓名,c.Name as 系,a.普研博士面试人次数,a.普研硕士面试人次数,a.EMBA面试人次数,a.MBA面试人次数,a.DBA面试人次数,a.工程硕士面试人次数,a.年度 from (select EmployeeID,[YEAR] as 年度,    ";
            sql += "isnull(sum(case Grade when 1 then StudentCount else 0 end),0) 普研硕士面试人次数, ";
            sql += "isnull(sum(case Grade when 2 then StudentCount else 0 end),0) 普研博士面试人次数, ";
            sql += "isnull(sum(case Grade when 3 then StudentCount else 0 end),0) DBA面试人次数,";
            sql += "isnull(sum(case Grade when 4 then StudentCount else 0 end),0) MBA面试人次数, ";
            sql += "isnull(sum(case Grade when 5 then StudentCount else 0 end),0) EMBA面试人次数, ";
            sql += "isnull(sum(case Grade when 6 then StudentCount else 0 end),0) 工程硕士面试人次数 ";
            sql += " from TeacherReview group by EmployeeID,[Year]) a inner join EmployeeInfo b on a.EmployeeID=b.EmployeeID ";
            sql += " left join Xi c on b.Xi_ID=c.Xi_ID ";
            sql += " where 1=1 ";
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
            switch (ViewState["showType"].ToString())
            {
                case "1": //查看当前系的所有的服务工作量
                    if (!VerificationHelper.IsNullOrEmpty(dict["Xi_ID"]))
                    {
                        sql += " and b.Xi_ID = @Xi_ID";
                        dal.ManagerParameter.AddNewInParameter("Xi_ID", ServerContext.AdminCurrentUser.Xi_ID);
                    }
                    break;
                case "2"://查看当前教师自己的服务工作量
                    if (!VerificationHelper.IsNullOrEmpty(dict["EmployeeID"]))
                    {
                        sql += " and b.EmployeeID = @EmployeeID";
                        dal.ManagerParameter.AddNewInParameter("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                    }
                    break;
            }
            if (ddlXi.SelectedValue != "-1")
            {
                sql += " and b.Xi_ID = @Xi_ID";
                dal.ManagerParameter.AddNewInParameter("Xi_ID", ddlXi.SelectedValue);
            }
            if (txt_Name.Text != "")
            {
                sql += " and b.Name like '%'+@Name+'%'";
                dal.ManagerParameter.AddNewInParameter("Name", txt_Name.Text);
            }
            //if (dict.ContainsKey("Year"))
            //{
            sql += " and a.年度 = @Year order by a.EmployeeID DESC ";
            dal.ManagerParameter.AddNewInParameter("Year", txt_Year.Value);
            //}

            
            NPOIHelper.ExportByWeb(dal.ExecuteFillDataTable(sql), string.Empty, "教师面试信息" + new Random().Next().ToString() + ".xls");
        }

        protected void linkBtnDeleSelect_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(hidDeleString.Value))
            {
                DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
                
                string value = hidDeleString.Value;
                IList<AdoCondition> delCollections = new List<AdoCondition>();
                delCollections.Add(new AdoCondition(CompareOper.IN, LogicOper.and, "EmployeeID", value));
                delCollections.Add(new AdoCondition(CompareOper.equal, LogicOper.and, "Year", hidYear.Value));
                int reValue = dal.ModifyData.Delete<TeacherReview>(delCollections);
                if (reValue == 1)
                {
                    IDictionary<String, object> dict = new Dictionary<string, object>();
                    switch (ViewState["showType"].ToString())
                    {
                        case "1": //查看当前教师自己的服务工作量
                            dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID);
                            break;
                        case "2": //查看当前系的所有的服务工作量
                            dict.Add("Xi_ID", ServerContext.AdminCurrentUser.Xi_ID);
                            break;
                    }
                    Bind(1, dict);
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.correct('数据删除成功');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.error('数据删除失败');", true);
                }
            }
        }

        protected void replData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label lb_Oper = (Label)e.Item.FindControl("lb_oper");
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string name = drv.Row.ItemArray[8].ToString();
                switch (ViewState["showType"].ToString())
                {
                    case "0":
                        lb_Oper.Text = "<a href=\"javascript:void(0);\" onclick=\"javascript:turnModifyPage($(this).parent().getTabRowId(),'修改教师面试信息','AuditionUpdate.aspx');return false;\">编辑</a>&nbsp;&nbsp; <a href=\"javascript:void(0);\" onclick=\"javascript:fastAdd($(this).parent().getTabRowId(),'" + name + "');return false;\">添加</a>";
                        break;
                    case "1":                  
                    case "2":
                        lb_Oper.Text = "<a href=\"javascript:void(0);\" onclick=\"javascript:turnModifyPage($(this).parent().getTabRowId(),'查看教师面试信息','AuditionShow.aspx');return false;\">查看</a>";
                        break;
                }
            }
        }
    }
}